package jprint.base.label;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import jprint.base.util.JsonUtil;

/**
 * @author zw-zheng
 * Created on 2020-11-29
 */
public class Label {

    private String name;
    private List<LabelItem> items;

    public static Label from(File file) {
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file),
                                                          StandardCharsets.UTF_8);
            Label label = JsonUtil.fromJsonString(new BufferedReader(isr), Label.class);
            isr.close();
            return label;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Label from(String json) {
        return JsonUtil.fromJsonString(json, Label.class);
    }

    public static Label from(Map<String, String> params) {
        if (params.containsKey("postData")) {
            return from(params.get("postData"));
        }
        return null;
    }

    public String getName() {
        return name;
    }

    public Label setName(String name) {
        this.name = name;
        return this;
    }

    public List<LabelItem> getItems() {
        return items;
    }

    public Label setItems(List<LabelItem> items) {
        this.items = items;
        return this;
    }
}
